package com.yhx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yhx.iservice.IBusinessService;
import com.yhx.utils.MessageConstant;
import com.yhx.utils.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("business")
public class BusinessController {
    @Reference
    private IBusinessService iBusinessService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @GetMapping
    public Result hotsetmeal(){
//        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
//        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        try {
//            Map<Object, Object> data = hash.entries("首页数据1");
//            Set<String> set = opsForva.members("首页数据2");
//            if(!data.isEmpty() || set.size()==3){
//                return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,data,set);
//            }

            String str = stringRedisTemplate.opsForValue().get("首页数据");

            if(str!=null){
                JSONObject jsonObject = JSON.parseObject(str);
                return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,jsonObject);
            }
            Map<String, Object> businessData = iBusinessService.getBusinessData();
            String s = JSON.toJSONString(businessData);
            stringRedisTemplate.opsForValue().set("首页数据",s);
            stringRedisTemplate.expire("首页数据",1, TimeUnit.MINUTES);
            String s1 = stringRedisTemplate.opsForValue().get("首页数据");
            JSONObject jsonObject = JSON.parseObject(s1);
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,businessData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
